$(document).ready(function () {
	registerBtn();
	//获取短信验证码 pointer-events: none; check-phone
	$("#getVerifyCode,#resendSpan").click(function(){
		/*var telphone = $("#telphone").val();*/
		var email = $.trim($("#email").val());
		if(isNull(email)){
            swal("警告","邮箱名不能为空","warning");
            return;
        } 
        var emailReg = new RegExp(LTS.ReExp.email);
        if(!emailReg.test(email)){
            swal("警告","邮箱格式不正确","warning");
            return;
        }
		/*if(isNull($.trim(telphone))){
			swal("警告","手机号不能为空","warning");
            return;
		}
		var telReg = new RegExp(LTS.ReExp.phone);
		if(!telReg.test(telphone)){
            swal("警告","手机格式不正确","warning");
            return;
        }*/
		var url = getRoot() + "/sendSms";
		$.post(url,{"email":email},function(json){
			if (json && json.success) {
				$("#getVerifyCode").removeClass().addClass("disabledget");
				timeCountdown(60);
				$("#timeSpan").show();
            } else {
                if (json) {
                    swal("登陆失败",json['message'], 'error');
                    $("#getVerifyCode").removeClass().addClass("check-phone");
                }
            }
		});
	});
    $(document).on("click", "#login-btn", function () {
        var email = $.trim($("#email").val());
        var password = $("#password").val();
        if(isNull(email)){
            swal("警告","邮箱名不能为空","warning");
            return;
        }
        var emailReg = new RegExp(LTS.ReExp.email);
        if(!emailReg.test(email)){
            swal("警告","邮箱格式不正确","warning");
            return;
        }

        if(isNull(password)){
            swal("警告","密码不能为空","warning");
            return;
        }
        password = $.trim(password);
        var length = password.length;
        if(length < 6 || length > 10){
            swal("警告","密码长度应该在6到10之间，包括6和10","warning");
            return;
        }
        var params = {};
        params['email'] = email;
        params['password'] = password;
        //验证线上环境
        if(smsLoginValidate == '2'){
        	var bol = smsLoginValidateLive(params);
        	if(!bol){
        		return;
        	}
        }
        var url = getRoot() + "/login";
        $.postHttp(params,url,function(json){
            if (json && json.success) {
                window.location.href= getRoot() + "/welcome"
            } else {
                if (json) {
                    swal("登陆失败",json['message'], 'error');
                }
            }
        });
    });

    /*敲击回车键*/
    $(function(){
        document.onkeydown = function(e){
            var ev = document.all ? window.event : e;
            if(ev.keyCode == 13) {
                $("#login-btn").trigger("click");
            }
        }
    });
    
    function timeCountdown(countdown){
    	var count = countdown;
    	if(count == 0){ 
            $("#timeSpan").hide();
            $("#getVerifyCode").hide();
            $("#resendSpan").show();
            $("#getVerifyCode").removeClass().addClass("check-phone");
        	$("#resendSpan").removeClass().addClass("ressend");
            return;
        }else{ 
        	$("#getVerifyCode").removeClass().addClass("disabledget");
        	$("#resendSpan").removeClass().addClass("disabledget");
        	$("#time").html( count-1 +" s后重新获取"); 
            --count;
            console.log(count);
        }
    	setTimeout(function(){timeCountdown(count)},1000);
    }
    
    //diamond是否为线上环境
    function smsLoginValidateLive(params){
    	/*var telphone = $("#telphone").val();*/
        var message_valid = $("#message_valid").val();
        /*if(isNull($.trim(telphone))){
			swal("警告","手机号不能为空","warning");
            return false;
		}*/
        message_valid = $.trim(message_valid);
        if(isNull(message_valid)){
        	swal("警告","验证码不能为空","warning");
            return false;
        }
        
        /*params['telphone'] = $.trim(telphone);*/
        params['messageValid'] = message_valid;
        return true;
    }
    
});

//注册按钮
function registerBtn(){
	$(document).on("click","#register-btn",function(){
		window.location.href = getRoot() + "/register/getResPage";
	});
}
